﻿function deleteImage(id, c) {
    if (c == 1) {
        var conf = confirm("Bạn có chắc chắn muốn xóa hình ảnh này ko?");
        if (conf == false) {
            return false;
        }
    }

    $j.ajax({
        type: "POST",
        url: "/WebServices/ProductImageService.asmx/DeleteImage",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            $j('#output #contentwrap_' + id + '').remove();
        },
        failure: function (msg) { console.log(msg); }
    });
}

function changeDefault(id) {
    $j.ajax({
        type: "POST",
        url: "/WebServices/ProductImageService.asmx/UpdateDefaultImage",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            // alert(response.d);
        },
        failure: function (msg) { console.log(msg); }
    });
}

function GetImages() {
    $j.ajax({
        type: "POST",
        url: "/WebServices/ProductImageService.asmx/GetImagesByProduct2",
        data: "{'productid':'" + id + "', 'action':'" + act + "', 'random':'" + random + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var product = response.d;
            Template(product);
        },
        failure: function (msg) {
            $j('#output').text(msg);
        }
    });
}

function Template(data) {
    document.getElementById('output').innerHTML = "";
    $j.each(data, function (index, d) {
        var str = "";
        var IsSelect = d.IsDefault == true ? "checked='checked'" : "";
        var path = mediadomain + "/Thumbnail.ashx?img=" + d.ImageFile + "&width=100&height=100";
        str += '<div class="contentwrap" id="contentwrap_' + d.Id + '">';
        str += '<div class="check"><input id="chk_' + d.Id + '" type="checkbox"></div>';
        str += '<div class="img"><img src="' + path + '"/></div>';
        str += '<div class="desc">' + d.Description + '</div>';
        str += '<div class="action"><a class="bigIcon imb_delete action-link-button" title="Xóa ảnh" onclick="deleteImage(\'' + d.Id + '\', 1); return false;" id="delete_' + d.Id + '" href="#"><i class="icon-trash"></i></a></div>';
        str += '<div class="pri"><input type="radio" onclick="changeDefault(\'' + d.Id + '\')" ' + IsSelect + ' value="' + d.Id + '" name="rdoDefault" id="rdo_' + d.Id + '"></div>';
        str += '</div>';
        $j('#output').append(str);
    });
}

$j(document).ready(function () {
    //trigger
    $j('#fix_upload').click(function () {
        //$j('#<%=upload_files.ClientID %>').click();
        $j('#Filedata').click();
    });

    //kendoui function
    $j('#Filedata').kendoUpload({
        async: {
            saveUrl: "/ProductUploadImage.aspx?id=" + id + "&act=" + act + "&random=" + random,
            autoUpload: true
        },
        select: function (e) {
            $j.each(e.files, function (index, value) {
                var regex = /\.(jpg|jpeg|png|gif)$/i;
                if (!regex.test(value.extension) || (value.size != null && value.size > 400 * 1024)) {
                    alert("Xin hãy chọn tập tin có định dạng: .jpg, .jpeg, .png, .gif và tối đa 400 KB");
                    e.preventDefault();
                }
            });
        },
        success: function (e) {
        },
        complete: function (e) {
            GetImages()
        },
        upload: function (e) { },
        multiple: true
    });

    //delete button
    $j('#delAll, #delAll2').click(function () {
        var conf = confirm("Bạn có chắc chắn muốn xóa hình ảnh này ko?");
        if (conf == false) return false;

        var items = $j('#output').find('.check input:checkbox:checked');
        $j.each(items, function (index, item) {
            var id = item.id.split('_')[1];
            deleteImage(id, 0);
        });
    });
    //check all function
    $j('#chkAll').click(function () {
        var items = $j('#output').find('.check input:checkbox');
        if ($j(this).is(':checked')) {
            items.each(function () { $j(this).attr('checked', true); });
        }
        else {
            items.each(function () { $j(this).attr('checked', false); });
        }
    });
    GetImages();

    //hide drag drop content
    if (!$j('.k-upload').hasClass('uploadhidden')) {
        $j('.k-upload').addClass('uploadhidden');
    }

    //Fix for safari that not allow multiple attribute html5
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('safari') != -1) {
        if (ua.indexOf('chrome') == -1) {
            $j('#Filedata').attr('multiple', 'multiple');
        }
    }
});